<template>
  <div class="bottom-bar">
    <div class="check-content">
      <check-button
        class="check-button"
        :Checked="isSelectAll"
        @click.native="selectALLClick"
      ></check-button>
      <span>全选</span>
    </div>
    <div class="price">合计:{{ totalPrice }}</div>
    <div class="calculate">
      <div class="calculatebgc" @click="calcClick">
        去结算({{ checkedLength }})
      </div>
    </div>
  </div>
</template>

<script>
import CheckButton from "components/content/checkButton/CheckButton.vue";
// 直接将vuex的getters转化为计算属性
import { mapGetters } from "vuex";

export default {
  name: "BottomBar",
  components: {
    CheckButton,
  },
  computed: {
    ...mapGetters(["cartList"]),
    totalPrice() {
      return (
        "￥" +
        this.cartList
          .filter((item) => item.checked == true)
          .reduce((preValue, item) => {
            return (preValue += item.price * item.count);
          }, 0)
          .toFixed(2)
      );
    },
    checkedLength() {
      return this.cartList.filter((item) => item.checked == true).length;
    },
    isSelectAll() {
      //   判断当前是都有元素没有被选中
      if (this.cartList.length == 0) {
        return false;
      } else return !this.cartList.find((item) => item.checked == false);
    },
  },
  methods: {
    calcClick() {
      // console.log("去结算");

      if (!this.checkedLength) {
        this.$toast.show("请选择购买的商品", 1000);
      }
    },
    selectALLClick() {
      //   console.log("全选");
      if (this.isSelectAll) {
        this.cartList.forEach((item) => {
          item.checked = false;
        });
      } else {
        this.cartList.forEach((item) => {
          item.checked = true;
        });
      }
    },
  },
};
</script>

<style scoped>
.bottom-bar {
  display: flex;
  position: absolute;
  width: 100%;
  height: 40px;
  bottom: 49px;
  z-index: 10;

  background-color: #eeedef;

  line-height: 40px;
}
.check-content {
  align-items: center;
  display: flex;
}
.check-button {
  height: 20px;
  width: 20px;
  line-height: 16px;
  margin-left: 20px;
  margin-right: 5px;
}
.price {
  padding-left: 10px;
  flex: 1;
}
.calculate {
  flex: 1;
  padding-left: 70px;
  text-align: center;
}
.calculatebgc {
  background-color: red;
}
</style>